home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 12482 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.9 KB

  1. Path: ix.netcom.com!news
  2. From: miker3@ix.netcom.com (Mike Rubenstein)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Settle a bet please
  5. Date: Sun, 31 Mar 1996 17:12:11 GMT
  6. Organization: Netcom
  7. Message-ID: <315ebb0b.443785980@nntp.ix.netcom.com>
  8. References: <4jfopb$o9n@news1.sympatico.ca> <KASPER.96Mar29073654@acm.org> <mjs.828111568@hubcap> <KASPER.96Mar30085147@acm.org>
  9. NNTP-Posting-Host: ix-dc13-29.ix.netcom.com
  10. X-NETCOM-Date: Sun Mar 31 11:14:49 AM CST 1996
  11. X-Newsreader: Forte Agent .99d/32.182
  12.  
  13. kasperowski@acm.org wrote:
  14.  
  15. > In article <mjs.828111568@hubcap> mjs@hubcap.clemson.edu (M. J. Saltzman) writes:
  16. >  | kasperowski@acm.org writes:
  17. >  | 
  18. >  | > char Name[8] = "My Name";
  19. >  | 
  20. >  | >is correct.  There's a \0 at the end of "My Name".
  21. >  | 
  22. >  | > char Name[] = "My Name";
  23. >  | 
  24. >  | >is better; the compiler does the counting for you.
  25. >  | 
  26. >  | Actually, all three are correct, they just mean different things.  First,
  27. >  | 
  28. >  |     char name[] = "My name";
  29. >  | 
  30. >  | and 
  31. >  | 
  32. >  |     char name[8] = "My name";
  33. >  | 
  34. >  | do mean the same thing: reserve 8 chars and initialize them to contain
  35. >  | 
  36. >  |     {'M', 'y', ' ', 'n', 'a', 'm', 'e', '\0'}.  
  37. >  | 
  38. >  | This means that name[] can be treated as a "C string" (i.e., a
  39. >  | null-terminated string) and used with the C string and i/o library
  40. >  | routines that understand about null-terminated strings.
  41. >  | 
  42. >  | But
  43. >  | 
  44. >  |     char name[7] = "My name";
  45. >  | 
  46. >  | reserves 7 chars, and initializes them to contain
  47. >  | 
  48. >  |     {'M', 'y', ' ', 'n', 'a', 'm', 'e'}.
  49. >  | 
  50. >  | This is perfectly legal C, but the resulting array of characters is
  51. > In private e-mail, someone said the same thing to me, and I replied,
  52. > "sure, I guess you're right."  I was wrong -- this is illegal.
  53. > In ANSI C, 
  54. >  char name[7] = "My name";
  55. > which is the same as
  56. >  char name[7] = {'M', 'y', ' ', 'n', 'a', 'm', 'e', '\0'};
  57. > is *not* legal.  Section A8.7 on p. 219 of Appendix A of my copy of
  58. > K&R's _The C Programming Language_, 2nd ed., says:
  59. >  If the array has fixed size, the number of initializers may not
  60. >  exceed the number of members of the array . . .
  61. > The line specifies an array of fixed size 7 chars, and then
  62. > initializes it with 8 chars.  This is illegal.
  63.  
  64. But the standard says otherwise.  From ISO 6.5.7:
  65.  
  66.     An array of character type may be initialized by a character 
  67.     string literal, optionally enclosed in braces.  Successive 
  68.     characters of the character string literal (including the 
  69.     terminating null character if there is room or if the array is
  70.  
  71.     of unknown size) initialze the elements of the array.
  72.  
  73. There also is an example (note: examples are not part of the
  74. standard):
  75.  
  76.     7. The declaration 
  77.  
  78.         char s[] = "abc", t[3] = "abc";
  79.  
  80.        defines "plain" chara rray objects s and t whose elements 
  81.        are initialized with character string literals.  This 
  82.        declaration is identical to
  83.  
  84.         char s[] = { 'a', 'b', 'c', '\0' },
  85.              t[] = { 'a', 'b', 'c' };
  86.  
  87.  
  88. Michael M Rubenstein
  89.